﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BDSimpleStatus.Models;

namespace BDSimpleStatus.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            LoginModel model = new LoginModel();
            model.Username = "Admin";
            return View(model);
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                HttpCookie cookie = Request.Cookies["BDSimpleStatus"];
                string ru = Request.QueryString["ReturnUrl"];
                Authentication auth = new Authentication();
                if (auth.Authenticate(model.Password))
                {
                    if (string.IsNullOrEmpty(ru) || ru.ToLower().Contains("http") || !ru.StartsWith("/") || ru.ToLower().Contains("ftp"))
                    {
                        return RedirectToAction("Index", "Home");
                    }
                    return Redirect(ru);
                }
                else
                {
                    ModelState.AddModelError("", "Invalid credentials supplied");
                }
                return View(model);

            }
            else
                return RedirectToAction("Index");
        }
    }
}
